package com.gupao.pattern.proxy.dynamicproxy.cglib;

import com.gupao.pattern.proxy.Person;
import com.gupao.pattern.proxy.dynamicproxy.jdk.Girl;
import com.gupao.pattern.proxy.dynamicproxy.jdk.custom.MeiPo;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibMeiPo implements MethodInterceptor {

    public Object getInstance(Class<?> clazz){

        //相当于Proxy，代理工具类
        Enhancer enhancer=new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        Object obj = enhancer.create();
        return obj;
    }



    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        System.out.println("我是媒婆：我要给你找对象，现在已经拿到你的需求");

        System.out.println("开始物色");
        Object obj = methodProxy.invokeSuper(o,objects);
        System.out.println("如果合适的话，准备办事");

        return obj;
    }

    public static void main(String[] args) {
        Girl person= (Girl) new CglibMeiPo().getInstance(Girl.class);
        System.out.println(person.findLove("张三", 18));
    }
}
